package com.ivy.irublesson.fragment;


import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;

import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.ivy.irublesson.R;
import com.ivy.irublesson.activity.CContentActivity;
import com.ivy.irublesson.adapter.CourseAdapter;
import com.ivy.irublesson.entity.Course;

import java.util.ArrayList;
import java.util.List;


public class CourseFragment extends Fragment{

    CourseAdapter adapter;
    private Activity context;
    private ListView list_view;
    private List<Course> allValues = new ArrayList<>();


    public CourseFragment(Activity context){
        this.context = context;
    }
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view  = inflater.inflate(R.layout.fragment_course,null);
        initCourses();
        list_view = (ListView) view.findViewById(R.id.list_view);
        //TODO

        adapter = new CourseAdapter(context , allValues);
        list_view.setAdapter( adapter );
        //list_view.setOnItemClickListener(this);

        //点击事件
        list_view.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(context,CContentActivity.class);
                startActivity(intent);
            }
        });
        //长按点击事件
        list_view.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view,final int position, long id) {
                //定义AlertDialog.Builder对象，当长按列表项的时候弹出确认删除对话框
                AlertDialog.Builder builder=new AlertDialog.Builder(getActivity());
                builder.setMessage("确定删除?");
                builder.setTitle("提示");

                //添加AlertDialog.Builder对象的setPositiveButton()方法
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if(allValues.remove(position)!=null){
                            System.out.println("success");
                        }else {
                            System.out.println("failed");
                        }
                        adapter.notifyDataSetChanged();
                        Toast.makeText(context, "删除列表项", Toast.LENGTH_SHORT).show();
                    }
                });

                //添加AlertDialog.Builder对象的setNegativeButton()方法
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                builder.create().show();
                return true;
            }
        });

        return view;
    }

//    @Override
//    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//
//        Intent intent = new Intent(context, CContentActivity.class);
//        startActivity(intent);
//    }


    private void initCourses(){
        for(int i = 0;i<10;i++){
            Course aa = new Course(01,"点灭⊕百城","001001");
            allValues.add(aa);
            Course ss = new Course(02,"百万亚瑟王","001002");
            allValues.add(ss);
            Course zz = new Course(03,"墨染＊殇泪","001003");
            allValues.add(zz);
            Course xx = new Course(04,"墨羽离殇△","001004");
            allValues.add(xx);
            Course gg = new Course(05,"放开那三国","001005");
            allValues.add(gg);
            Course yy = new Course(06,"浅末年代Ω","001006");
            allValues.add(yy);

        }
    }
}
